package basic.Object;

import java.util.concurrent.TimeUnit;

public class TestObject {
	public static void main(String[] args) throws InterruptedException {
		//Object obj = new Object();
		TestObject testObj = new TestObject();
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					System.out.println("this thread1 is processing");
					testObj.f();//设置线程阻塞10秒
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("this thread1 is over");
			}
		}).start();
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					System.out.println("this thread2 is processing");
					testObj.f();//设置线程阻塞10秒
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("this thread2 is over");
			}
		}).start();
		TimeUnit.MILLISECONDS.sleep(500);//等待上面两个线程进入阻塞期释放testObj对象锁
		testObj.f2();//主线程可以顺利获得testObj对象锁,同时解除阻塞
		System.out.println("main thread is over");
	}
	public void f() throws InterruptedException{
		synchronized (this) {
			this.wait(10000);
		}
	}
	public void f2() throws InterruptedException{
		synchronized (this) {
			this.notify();//随机解除一个线程的阻塞
			//this.notifyAll();//解除所有线程的阻塞
		}
	}
}
